package com.umfwechat.util;

import java.io.File;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.umfwechat.util.email.MailSenderInfo;
import com.umpay.operation.util.StringUtil;

/** 
 * desc:解析邮件模版
 * <p>创建人：liucong 创建日期：Nov 18, 2014 </p>
 * @version V1.0  
 */
public class EmailXmlUtil{
	
	private static Logger logger = LoggerFactory.getLogger(EmailXmlUtil.class);
	
	
	/**
	 * desc:解析邮件模版
	 * <p>创建人：liucong , Nov 25, 2014 5:59:04 PM</p>
	 * @param fileName
	 * @param mailSenderInfo
	 * @param map
	 */
	public static void emailXmlRead(String fileName,MailSenderInfo mailSenderInfo,Map<String,String> map){
		 try {
		//从 XML 文档获取 DOM 文档实例。使用此类，应用程序员可以从 XML 获取一个Document
		//创建解析工厂指定DocumentBuilder  
		 DocumentBuilder db=DocumentBuilderFactory.newInstance().newDocumentBuilder();
		 Document document=db.parse(new File(fileName));//把文件解析成DOCUMENT类型
		//  Element root = document.getDocumentElement();  //得到Document的根（节点名：book）
		// System.out.println("根节点标记名：" + root.getTagName());  
		 Element root=document.getDocumentElement() ;
		 String rootName=root.getNodeName();
		 NodeList list=document.getElementsByTagName(rootName);
		 showElem(list,mailSenderInfo,map);       
		} catch (Exception e) {
			logger.error("解析邮件模版异常:"+e);
		}
		   
		}
	
	
	public static void showElem(NodeList nl,MailSenderInfo mailSenderInfo,Map<String,String> map){
		for(int i=0;i<nl.getLength();i++){
		         Node n=nl.item(i);//得到父节点
		         if(n.hasChildNodes())
		         {
		         NamedNodeMap attributes=n.getAttributes(); //遍历节点所有属性如<dbstore single="false" att="tta"> 
		         for(int j=0;j<attributes.getLength();j++)
		         {
		          Node attribute=attributes.item(j);
		           //得到属性名
		          String attributeName=attribute.getNodeName();
		          //得到属性值
		          String attributeValue=attribute.getNodeValue();
		      }
		         }
		         //子节点
		      NodeList childList=n.getChildNodes();
		      for(int x=0;x<childList.getLength();x++)
		      {
		        Node childNode=childList.item(x);
		        //判断取出的值是否属于Element元素,目的是过滤掉
		        if(childNode instanceof Element)
		           {
		               //得到子节点的名字
		               String childNodeName=StringUtil.trim(childNode.getNodeName());
		               //得到子节点的值
		               String childNodeValue=StringUtil.trim(childNode.getTextContent());
		            if(childNodeName.equals(MailSenderInfo.HOST)){
		       			mailSenderInfo.setMailServerHost(childNodeValue);
		       		}else if(childNodeName.equals(MailSenderInfo.FROMNAME)){
		       			mailSenderInfo.setFromName(childNodeValue);
		       		}else if(childNodeName.equals(MailSenderInfo.CONTENT)){
		       			mailSenderInfo.setContent(childNodeValue);
		       		}else if(childNodeName.equals(MailSenderInfo.FROMADDR)){
		       			mailSenderInfo.setFromAddress(childNodeValue);
		       		}else if(childNodeName.equals(MailSenderInfo.SUBJECT)){
		       			mailSenderInfo.setSubject(childNodeValue);
		       		}else if(childNodeName.equals(MailSenderInfo.NAME)){
		       			mailSenderInfo.setUserName(childNodeValue);
		       		}else if(childNodeName.equals(MailSenderInfo.PASS)){
		       			mailSenderInfo.setPassword(childNodeValue);
		       		}
		           }
		      } showElem(n.getChildNodes(),mailSenderInfo,map);//递归
		          
		         }
		}
}
